<?php
namespace view;

class UserInfo {
	private static $cookieUserName = "UserName";
	private static $cookiePassword = "Password";
	private static $sessionLoggedIn = "loggedin";
	private static $serverRequestMeth = "REQUEST_METHOD";
	private static $postRememberBox = "rememberBox";
	private static $postUName = "uName";
	private static $postPWord = "pWord";
	private static $arrayusername = "username";
	private static $arraypassword = "password";
	
	/**
	 * @return boolean
	 */
	public function cookieIsset() {
		return isset($_COOKIE[self::$cookieUserName]);
	}

	/**
	 * @return boolean
	 */
	public function sessionSet() {
		return isset($_SESSION[self::$sessionLoggedIn]);
	}

	/**
	 * @return boolean
	 */
	public function serverRequestMethod() {
		return $_SERVER[self::$serverRequestMeth];
	}

	/**
	 * @return array
	 */
	public function getCookieInfo() {
		return [
			self::$arrayusername => $_COOKIE[self::$cookieUserName],
			self::$arraypassword => $_COOKIE[self::$cookiePassword]
		];
	}

	/**
	 * @return boolean
	 */
	public function getRememberData() {
		return isset($_POST[self::$postRememberBox]);
	}

	/**
	 * @return string
	 */
	public function getUserName() {
		return $_POST[self::$postUName];
	}

	/**
	 * @return string
	 */
	public function getUserPW() {
		return $_POST[self::$postPWord];
	}

	public function saveUserToCookie() {
		$usrName = $this->getUserName();
		$usrPWord = $this->getUserPW();

		setcookie(self::$cookieUserName, $usrName, time()+3600*24*30);
		$encryptedPassWord = hash("sha512", $usrPWord);
		setcookie(self::$cookiePassword, $encryptedPassWord, time()+3600*24*30);
	}

	public function removeUsr() {
		if($this->cookieIsset()) {
			unset($_COOKIE[self::$cookieUserName]);
			setcookie(self::$cookieUserName, "", time() - 3600*24*30);
		}
		if(isset($_COOKIE[self::$cookiePassword])) {
			unset($_COOKIE[self::$cookiePassword]);
			setcookie(self::$cookiePassword, "", time() - 3600*24*30);
		}
	}
}
?>